<?php
class upload {

	private static $_instance = null;

	/**
	 * 获取实体
	 * @method getInstance
	 * @return [type]      [description]
	 * @author NewFuture
	 */
	private static function getInstance()
	{
		if (null === self::$_instance)
		{
			self::$_instance=new self();
		}
		return self::$_instance;
	}

	/**
	 * 错误信息
	 * @method checkFilesError
	 * @param  [string] $files   [description]
	 * @author NewFuture
	 */
	public static function checkFilesError($files)
	{    
		$checkFile = new stdClass();
    	$checkFile->value=true;
    	$checkFile->result='';
    	$error='';
		if($files!="undefined" && !empty($files['error'])){
			switch($files['error']){
				case '1':
					$error = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值！';
					break;
				case '2':
					$error = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值！';
					break;
				case '3':
					$error = '文件只有部分被上传！';
					break;
				case '4':
					$error = ' 没有文件被上传！';
					break;
				case '6':
					$error = ' 找不到临时文件夹！';
					break;
				case '7':
					$error = ' 文件写入失败';
					break;
				default:
					$error = '未知错误！';
					break;
			}
			//检查临时目录
		}else if(empty($files['tmp_name']) || $files['tmp_name'] == 'none'){
			$error = ' 没有文件被上传！';
		}
		if(!empty($error)){
			$checkFile->result = $error;
			$checkFile->value = false;
		}
		return $checkFile;
	}
	/**
	 * 检查保存文件的目录是否存在
	 * @method checkSaveDir
	 * @param  [string] $files   [description]
	 * @author NewFuture
	 */
	public static function createSaveDir($dir,$auth)
	{ 

    	$checkDir = new stdClass();
    	$checkDir->value=true;
		if(!file_exists($dir)){
			$createResult = mkdir($dir,$auth,true);
			$checkDir->value = !$createResult?false:true;
			$checkDir->result=!$createResult?"目录创建失败":"";
		}else{
			chmod($dir,$auth); //设置文件权限
			$checkDir->result="";
		}
    	return $checkDir;
	}
	 /*
	 * 随机生成图片名称guid
	 * @method guid
	 * @param  [string] $files   [description]
	 * @author NewFuture
	 */
	public static function guid()
	{
		if (function_exists('com_create_guid')){
        		return com_create_guid();
	    }else{
	        mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
	        $charid = strtoupper(md5(uniqid(rand(), true)));
	        $hyphen = chr(45);// "-"
	        $uuid = substr($charid, 0, 8).$hyphen
	                .substr($charid, 8, 4).$hyphen
	                .substr($charid,12, 4).$hyphen
	                .substr($charid,16, 4).$hyphen
	                .substr($charid,20,12);
	        return strtolower($uuid);
    	}
	}
	// 获取文件类型
    public static function getExt($files)
    {
        $extStr = explode('.', $files);
        $count = count($extStr)-1;
        return $extStr[$count];
    }
     //删除文件
    public static function deleteFile($paths,$root){
    	if(!empty($paths)){
	    	$count = count($paths);
	    	for($i=0;$i<$count;$i++){
	    		if(file_exists($root.$paths[$i])){	
	    			unlink($root.$paths[$i]);
	    		}
	    	}
    	}
    }
    /**
	 * 将图片从临时文件移动到创建的目录下
	 * @method moveUploadedFile
	 * @param  [string] $files   [description]
	 * @author NewFuture
	 */
	public static function moveUploadedFile($files,$dir,$auth)
	{
		
		$fileType = self::getExt($files['name']);
		$fileName = self::guid().'.'.$fileType;
		$stored_path = $dir.$fileName;
		$upStatus = new stdClass();
		$upStatus->value=true;
    	if(move_uploaded_file($files['tmp_name'],$stored_path)){
    		$upStatus->result=$fileName;
    	}else{
    		$upStatus->value=false;
    		$upStatus->result="文件上传失败";
    	}
    	return $upStatus;
	}
	public static function resourceDir($pathType){
		return Yaf_Application::app()->getConfig()->$pathType->dir;
	}
	//删除文件两个路径
    public static function deleteBothFile($paths,$root,$otherRoot){
    	if(!empty($paths)){
	    	$count = count($paths);
	    	for($i=0;$i<$count;$i++){
	    		if(file_exists($root.$paths[$i])){	
	    			unlink($root.$paths[$i]);
	    		}else if(file_exists($otherRoot.$paths[$i])){
	    			unlink($otherRoot.$paths[$i]);
	    		}
	    	}
    	}
    }
}